package org.edu.modules.system.service.impl;

import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.edu.modules.system.entity.ProcessStep;
import org.edu.modules.system.entity.ProcessType;
import org.edu.modules.system.entity.SysDataLog;
import org.edu.modules.system.mapper.ProcessStepMapper;
import org.edu.modules.system.mapper.ProcessTypeMapper;
import org.edu.modules.system.mapper.SysDataLogMapper;
import org.edu.modules.system.service.IProcessStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author lbh
 * @Date 2025/2/13 上午9:12
 * @Version 1.0
 */
@Slf4j
@Service
public class ProcessStepServiceImpl extends ServiceImpl<ProcessStepMapper, ProcessStep> implements IProcessStepService {
    @Autowired
    private ProcessStepMapper processStepMapper;
    @Autowired
    private ProcessTypeMapper processTypeMapper;
    @Autowired
    private  ObjectMapper objectMapper;

    @Transactional
    public void saveProcessStep(ProcessType processType,List<ProcessStep> steps) {
        // 保存流程类型
        processTypeMapper.saveProcessType(
                processType.getId(),
                processType.getName(),
                processType.getDescription(),
                processType.getVersion(),
                processType.getIsActive(),
                processType.getShowColumn()
        );
        // 获取新流程类型ID（假设使用数据库自增ID）
        if (processType.getId() == null) {
            // 需要根据实际情况获取最新ID
            Integer newId = processStepMapper.getLastInsertId();
            processType.setId(newId);
        }

        // 保存流程步骤
        try {
            String stepsJson = objectMapper.writeValueAsString(steps);
            processStepMapper.saveProcessStep(
                    processType.getId(),
                    stepsJson
            );
        } catch (JsonProcessingException e) {
            throw new RuntimeException("步骤配置序列化失败", e);
        }
    }
}
